package cs236369.hw5;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public abstract class Post {
	public Post(int id, String owner, Date creationTime, String name, int capacity,
			String content, double latitude, double longitude,
			Skill[] skills) throws RescueMeException {
		if(owner == null || owner.equals("")) {
			throw new RescueMeException("illegal parameter", "ownew is empty");
		}
		if(latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180) {
			throw new RescueMeException("illegal parameter", "incorrect position");
		}
		this.id = id;
		this.owner = owner == null ? "" : owner;
		this.creationTime = creationTime == null ? new Date(new java.util.Date().getTime()) : creationTime;
		this.name = name == null ? "" : name;
		this.capacity = capacity;
		this.content = content == null ? "" : content;
		this.latitude = latitude;
		this.longitude = longitude;
		this.skills = skills;
	}
	
	public int getId() {
		return id;
	}
	public String getOwner() {
		return owner;
	}
	public Date getCreationTime() {
		return creationTime;
	}
	public int getCapacity() {
		return capacity;
	}
	public String getContent() {
		return content;
	}
	public double getLatitute() {
		return latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public Skill[] getSkills() {
		return skills;
	}
	public String getName() {
		return name;
	}
	public String getCreationString() {
		java.util.Date date = new java.util.Date(creationTime.getTime());
		return new SimpleDateFormat("MM/dd/yyyy").format(date);
	}
	
	public boolean doesHaveSkill(int skillId) {
		for(int i = 0; i < skills.length; i++) {
			if(skills[i].getId() == skillId) { return true; }
		}
		return false;
	}
	
	/**
	 * Returns a string that represent a list of all the post IDs in the ArrayList
	 * @param posts - the list of posts which IDs we want
	 * @return A string in the format: ID1,ID2,...,IDn
	 */
	public static String getIDsString(ArrayList<Post> posts){
		String strIDs="";
		boolean isFirst = true;
		for(Post p : posts){
			if(isFirst){
				isFirst=false;
			}
			else{
				strIDs += ",";
			}
			strIDs += p.getId();
		}
		return strIDs;
	}
	
	protected int id;
	protected String owner;
	protected Date creationTime;
	protected String name;
	protected int capacity;
	protected String content;
	protected double latitude;
	protected double longitude;
	protected Skill[] skills;
}
